<?php
require_once 'EZend/Controller/AdminBaseController.php';

class Admin_ArticleController extends AdminBaseController {
	private $_perpage = 30;
	
	public function preDispatch() {
		parent::preDispatch();
		$this->_helper->layout->setLayout('iframe');
	}
	
	public function indexAction() {
		$articles = $this->getCmsArticleService()->listInPage(1, $this->_perpage);
		$this->view->assign('articles', $articles);
		
		$this->view->assign('categories', $this->getCategories());
	}
	
	public function addAction() {
		$this->view->assign('categories', $this->getCategories());
		
		$this->view->assign('editor', $this->getCkEditor());
	}
	
	public function doaddAction() {
		if (!$this->getRequest()->isPost()) return $this->showNotice('非法请求');
		$article = array();
		$article['category_id'] = intval($this->getRequest()->getPost('category_id'));
		$article['title'] = trim($this->getRequest()->getPost('title'));
		$article['description'] = trim($this->getRequest()->getPost('description'));
		$article['content'] = trim($this->getRequest()->getPost('content'));
		
		if ($article['category_id'] <= 0 || '' == $article['title'] || '' == $article['description'] || '' == $article['content']) return $this->showNotice('请填写必要项');
		
		if (!$this->getCmsArticleService()->add($article, $_FILES['image'], $this->getCurrentAdminUser()->getUserInfo())) return $this->showNotice('添加文章失败，请重试');
		return $this->showNotice('添加文章成功', $this->getInnerUrl());
	}
	
	public function editAction() {
		$articleId = intval($this->getRequest()->getParam('id'));
		$article = $this->getCmsArticleService()->get($articleId);
		if (empty($article)) return $this->showNotice('找不到文章，请重试');
		
		$this->view->assign('article', $article);
		$this->view->assign('categories', $this->getCategories());
		
		$this->view->assign('editor', $this->getCkEditor());
	}
	
	public function doeditAction() {
		if (!$this->getRequest()->isPost()) return $this->showNotice('非法请求');
		
		$articleId = intval($this->getRequest()->getParam('id'));
		$article = array();
		$article['category_id'] = intval($this->getRequest()->getPost('category_id'));
		$article['title'] = trim($this->getRequest()->getPost('title'));
		$article['description'] = trim($this->getRequest()->getPost('description'));
		$article['content'] = trim($this->getRequest()->getPost('content'));
		
		if (!$this->getCmsArticleService()->update($articleId, $article, $_FILES['image'])) return $this->showNotice('所有信息均未修改');
		return $this->showNotice('修改文章成功', $this->getInnerUrl());
	}
	
	public function deleteAction() {
		$articleId = intval($this->getRequest()->getParam('id'));
		
		if (!$this->getCmsArticleService()->delete($articleId)) return $this->showNotice('删除文章失败');
		return $this->showNotice('删除文章成功', $this->getInnerUrl());
	}
	
	
	private function getCategories() {
		$groups = $this->getCmsArticleService()->listAllCategories();
		$categories = array();
		
		return $this->loopGenerateCategories($groups, $categories);
	}
	
	private function loopGenerateCategories($groups, $categories, $parentId = 0, $depth = 0) {
		if (!isset($groups[$parentId])) return $categories;
		
		$prefix = $depth > 0 ? str_repeat('   ', $depth - 1) . '|--' : '';
		$depth++;
		foreach ($groups[$parentId] as $item) {
			$categories[$item['id']] = $prefix . $item['title'];
			if (isset($groups[$item['id']])) $categories = $this->loopGenerateCategories($groups, $categories, $item['id'], $depth);
		}
		return $categories;
	}
	
	private function getCkEditor() {
		$config = include APPLICATION_PATH . '/configs/cms/ck.php';
		require_once $config['editorRequire'];
		require_once $config['finderRequire'];
		
		$editor = new CKEditor();
		$editor->basePath = $config['editorUrl'];
		CKFinder::SetupCKEditor($editor, $config['finderUrl']);
		
		return $editor;
	}
	
	
	
	private function getCmsArticleService() {
		require_once 'Models/Cms/Service/CmsServiceFactory.php';
		return CmsServiceFactory::getInstance()->createCmsArticleService();
	}
}